﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_QC.QA_ViewModel;//for mvvm




namespace WPF_QC.QA_View
{
    /// <summary>
    /// Interaction logic for MeasurementEntry.xaml
    /// </summary>
    public partial class MeasurementEntry : UserControl
    {
        public decimal EnterMeasurement { get; set; }
        public string ScheduledDate { get; set; }
        public string MaterialID { get; set; }
        public string OrderID { get; set; }
        public string QCAttribName { get; set; }
       
        private ServiceReference2.Service1Client serviceClient = new ServiceReference2.Service1Client();
       
        public MeasurementEntry()
        {
            InitializeComponent();
            //this.Loaded += (s, e) => { this.DataContext = this.viewModel; }; for mvvm
        }

        private void testbutton_Click(object sender, RoutedEventArgs e)
        {
                      
       
        }

        private void materialstomeasure_combo_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable ds = new DataTable();//create table
            try
            {
                ds = serviceClient.ListofMaterialsScheduled();//call method to get data
                materialstomeasure_combo.ItemsSource = ds.DefaultView;//display in datagrid
                materialstomeasure_combo.DisplayMemberPath = ds.Columns["OrderID"].ToString();
            }
            catch (Exception ex)
            {
                machineNameTextBlock.Text = ex.Message;
            }

        }
        private void materialstomeasure_combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int orderid = 0;
            
            if (materialstomeasure_combo.SelectedItem is DataRowView)
            {
                orderid = Convert.ToInt32(((DataRowView)materialstomeasure_combo.SelectedItem).Row["MaterialID"]);
            }


            materialtomeasure_dg.ItemsSource = serviceClient.DisplayRequiredMeasurements(orderid).DefaultView;

            DataTable measures = serviceClient.DisplayRequiredMeasurements(orderid);
        }

        


        private void submitEntryBtn_Click(object sender, RoutedEventArgs e)
        {
            materialtomeasure_dg.CommitEdit();

           DataTable dt = new DataTable("name");
           dt = ((DataView)materialtomeasure_dg.ItemsSource).Table;

           if ((dt.Columns["EnterMeasurement"] != null) ||
               (IsNumeric(dt.Columns["EnterMeasurement"])) ||
               (dt != null)
               )
            
           {
               machineNameTextBlock.Text = serviceClient.GetMeasurementsTaken_table(dt);
           }
           else
           { machineNameTextBlock.Text = "Measurement must be number and can't not be empty"; };
           }




        public static bool IsNumeric(System.Data.DataColumn col)
        {
            if (col == null)
                return false;
            // Make this const
            var numericTypes = new[] { typeof(Byte), typeof(Decimal), typeof(Double),
        typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte),
        typeof(Single), typeof(UInt16), typeof(UInt32), typeof(UInt64)};
            return numericTypes.Contains(col.DataType);


    

    }

}
}
